package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.WorkOrderProcessService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.WorkOrderProcessForm;
import com.youlai.boot.factory.model.query.WorkOrderProcessQuery;
import com.youlai.boot.factory.model.vo.WorkOrderProcessVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 报工工序明细前端控制层
 *
 * @author youlaitech
 * @since 2025-10-16 15:34
 */
@Tag(name = "报工工序明细接口")
@RestController
@RequestMapping("/api/v1/work-order-process")
@RequiredArgsConstructor
public class WorkOrderProcessController  {

    private final WorkOrderProcessService workOrderProcessService;

    @Operation(summary = "报工工序明细分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:work-order-process:query')")
    public PageResult<WorkOrderProcessVO> getWorkOrderProcessPage(WorkOrderProcessQuery queryParams ) {
        IPage<WorkOrderProcessVO> result = workOrderProcessService.getWorkOrderProcessPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增报工工序明细")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:work-order-process:add')")
    public Result<Void> saveWorkOrderProcess(@RequestBody @Valid WorkOrderProcessForm formData ) {
        boolean result = workOrderProcessService.saveWorkOrderProcess(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取报工工序明细表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:work-order-process:edit')")
    public Result<WorkOrderProcessForm> getWorkOrderProcessForm(
        @Parameter(description = "报工工序明细ID") @PathVariable Long id
    ) {
        WorkOrderProcessForm formData = workOrderProcessService.getWorkOrderProcessFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改报工工序明细")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:work-order-process:edit')")
    public Result<Void> updateWorkOrderProcess(
            @Parameter(description = "报工工序明细ID") @PathVariable Long id,
            @RequestBody @Validated WorkOrderProcessForm formData
    ) {
        boolean result = workOrderProcessService.updateWorkOrderProcess(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除报工工序明细")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:work-order-process:delete')")
    public Result<Void> deleteWorkOrderProcesss(
        @Parameter(description = "报工工序明细ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = workOrderProcessService.deleteWorkOrderProcesss(ids);
        return Result.judge(result);
    }
}
